package control;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import database.MaConnexion;

/**
 * Servlet implementation class ServletDataBase
 */
public class ServletDataBase extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public ServletDataBase() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doRequest(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO doRequest acc�s bd
		try {
//			Class.forName(this.getInitParameter("driver"));
//
//			Connection connexion = DriverManager.getConnection(
//					this.getInitParameter("protocol") + "://"
//							+ this.getInitParameter("host") + "/"
//							+ this.getInitParameter("db"),
//					this.getInitParameter("username"),
//					this.getInitParameter("password"));

			// traitement
			// r�cup�ration d'un statement
			Statement stm = MaConnexion.getInstance().getConnexion().createStatement();

			// r�sultat de la requ�te
			ResultSet rs = stm
					.executeQuery("SELECT name, price FROM produit");

			PrintWriter out = response.getWriter();
			while (rs.next()) {
				out.println( rs.getString("name") + " " + rs.getDouble("price"));
			}

		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doRequest(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doRequest(request, response);
	}

}
